using System.Configuration;

namespace OpenWaves
{
    public class OpenWavesFileLoggingConfigurationSectionWrapper : IFileLoggingServiceConfiguration
    {
        private readonly LoggingConfigurationSection configSection;
        private readonly object mwCommonConfigSection;

        public OpenWavesFileLoggingConfigurationSectionWrapper()
        {
            this.configSection = (LoggingConfigurationSection)ConfigurationManager.GetSection("openWaves.logging");
            if (this.configSection == null)
            {
                this.mwCommonConfigSection = ConfigurationManager.GetSection("makingWaves.common");
                this.configSection = new LoggingConfigurationSection();
            }
        }

        public string LogDirectory
        {
            get
            {
                if (this.mwCommonConfigSection != null)
                {
                    var logDirectoryProperty = this.mwCommonConfigSection.GetType().GetProperty("LogDirectory");
                    if (logDirectoryProperty != null)
                    {
                        return (string) logDirectoryProperty.GetValue(this.mwCommonConfigSection, new object[0]);
                    }
                }

                return this.configSection.LogDirectory;
            }
        }

        public LogEntrySeverity LogSeverityThreshold
        {
            // Try to maintain backwards compatibility with makingWaves.common config section schema
            get
            {
                if (this.mwCommonConfigSection != null)
                {
                    var debugLogEnabledProperty = this.mwCommonConfigSection.GetType().GetProperty("DebugLogEnabled");
                    if (debugLogEnabledProperty != null)
                    {
                        return (bool) debugLogEnabledProperty.GetValue(this.mwCommonConfigSection, new object[0])
                                    ? LogEntrySeverity.Debug
                                    : LogEntrySeverity.Warning;
                    }
                }

                return this.configSection.LogSeverityThreshold;
            }
        }
    }
}